package com.beta.pay.yeepay.request;

import org.apache.commons.collections.CollectionUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by vector on 2016/12/12.
 */
public class RefundRequest extends BaseRequest {

    private static final long serialVersionUID = -4375875924603210083L;

    /**
     * 商户订单号：已完成支付的订单号
     */
    private String orderRequestId;

    private BigDecimal amount;

    /**
     * 确认退款：固定值 1
     */
    private String confirm = "1";

    /**
     * 退款备注
     */
    private String memo;

    /**
     * 子账户退款详情
     * 格式： A商户编号:0.001|B 商户编号:0.06
     */
    private List<DivideInfo> divideInfos;

    public RefundRequest(String requestId) {
        super(requestId);
    }

    public String getOrderRequestId() {
        return orderRequestId;
    }

    public RefundRequest setOrderRequestId(String orderRequestId) {
        this.orderRequestId = orderRequestId;
        return this;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public RefundRequest setAmount(BigDecimal amount) {
        this.amount = amount;
        return this;
    }

    public String getConfirm() {
        return confirm;
    }

    public RefundRequest setConfirm(String confirm) {
        this.confirm = confirm;
        return this;
    }

    public String getMemo() {
        return memo;
    }

    public RefundRequest setMemo(String memo) {
        this.memo = memo;
        return this;
    }

    public List<DivideInfo> getDivideInfos() {
        return divideInfos;
    }

    public RefundRequest setDivideInfos(List<DivideInfo> divideInfos) {
        this.divideInfos = divideInfos;
        return this;
    }

    public String divideInfoString() {
        if (CollectionUtils.isEmpty(this.divideInfos)) {
            return "";
        }

        return divideInfos.stream().map(DivideInfo::toString).collect(Collectors.joining("|"));
    }

    public static class DivideInfo {

        /**
         * 子账号
         */
        private String ledgerCode;

        /**
         * 退款比例
         */
        private BigDecimal ratio;

        public DivideInfo(String ledgerCode, BigDecimal ratio) {
            this.ledgerCode = ledgerCode;
            this.ratio = ratio;
        }

        public String getLedgerCode() {
            return ledgerCode;
        }

        public BigDecimal getRatio() {
            return ratio;
        }

        public String toString() {
            return this.ledgerCode + ":" + this.ratio;
        }

    }

}
